<template>
  <div class="zsh-modal" v-if="visible">
    <div class="box">
      <div class="header">
        <div class="tabs">
          <div class="tab" @click="tabClick(index)" :class="{active:tabs.length > 1 && index == tabActive}" v-for="(item,index) in tabs" v-text="item.name"></div>
        </div>
        <div class="search">
          <input v-model="keyword" placeholder="搜索"></input>
        </div>
        <div class="close-btn" @click="visible = false">
          <i class="el-icon-close"></i>
        </div>
      </div>
      <div class="list">
        <div class="item" v-for="item in result" @click="itemClick(item)">
          <div class="avatar" v-text="item.name.substr(0,1)">
          </div>
          <div class="info">
            <div class="name" v-text="item.name"></div>
            <div class="contact" v-if="item.email || item.mobile">
              <div class="email" v-text="item.email"></div>
              <div class="gap" v-if="item.mobile"></div>
              <div class="mobile" v-text="item.mobile"></div>
            </div>
          </div>
        </div>
        <div v-if="result.length == 0" style="text-align: center;line-height: 60px;font-size:14px;color:#666666">
          <i v-if="loading" style="font-size: 26px;margin-top: 30px" class="el-icon-loading"></i>
          <span v-else>暂无查询结果</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {findPeopleList} from "@/api/auth/tenant";
import {getDeptList} from "@/api/auth/dept";
import {findCustomerList} from "@/api/customer";

export default {
  name: "zsh-search-global",
  data() {
    return {
      visible: false,
      title:'',
      mode:'',
      keyword:'',
      result:[],
      loading:false,
      tabs:[],
      tabActive:-1
    }
  },
  methods: {
    show(mode) {
      this.mode = mode
      switch (mode) {
        case 'people':
          this.tabs = [{name:'分组',action:this.deptSearch},{name:'用户',action:this.peopleSearch}]
          break
        default:
          this.mode = 'customer'
          this.tabs = [{name:'查询联系人',action:this.customerSearch}]
      }
      this.result = []
      this.tabActive = 0
      this.tabs[0].action()
      this.visible = true
    },
    tabClick(index) {
      if(this.tabActive == index) {
        return
      }
      this.result = []
      this.tabActive = index
      this.tabs[index].action()
    },
    itemClick(item) {
      this.visible = false
      this.$emit('select',this.mode,item)
    },
    customerSearch() {
      this.loading = true
      findCustomerList().then(res => {
        if(res.code == 200) {
          this.result = res.data
        }
      }).finally(() => {
        this.loading = false
      })
    },
    peopleSearch() {
      this.loading = true
      findPeopleList().then(res => {
        if(res.code == 200) {
          res.data.forEach(function (item) {
            item.name = item.nickname
          })
          this.result = res.data
        }
      }).finally(() => {
        this.loading = false
      })
    },
    deptSearch() {
      this.loading = true
      getDeptList().then(res => {
        if(res.code == 200) {
          this.result = res.data
        }
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped lang="less">
.zsh-modal {
  position: fixed;
  top: 40px;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0,0,0,0.3);
  z-index: 99;
  .box {
    margin: 0 auto;
    width: 679px;
    background: #FFFFFF;
    @media(max-width: 679px) {
      width: 100%;
    }
    .header {
      height: 50px;
      display: flex;
      line-height: 50px;
      align-items: center;
      border-bottom: 1px solid #F0F0F0;
      color: #666666;
      .tabs {
        padding: 0 25px;
        flex: 1;
        display: flex;
        .tab {
          cursor: pointer;
          margin-right: 10px;
          padding: 0 10px;
          &.active {
            border-bottom: 1px solid #0091ff;
          }
        }
      }
      .search {
        display: none;
        input {
          height: 30px;
          line-height: 30px;
          padding:0 5px;
          display: inline;
          box-sizing: border-box;
          font-size: 18px;
          outline: none;
          box-shadow: none;
          background-image: none;
          border: none;
          width: 50px;
          transition: all .3s;
          &:hover {
            width: 150px;
            border-bottom: 1px solid #E8E8E8;
            @media(max-width: 679px) {
              width: 80px;
            }
          }
          &:focus {
            width: 150px;
            @media(max-width: 679px) {
              width: 80px;
            }
            border-bottom: 1px solid #0091FF;
          }
          &::-webkit-input-placeholder {
            font-size: 16px;
            color: #E8E8E8;
          }
        }
      }
      .close-btn {
        padding: 0 15px;
        margin: 0 10px;
        &:hover {
          cursor: pointer;
          color: #0091ff;
        }
      }
    }
    .list {
      height: 400px;
      overflow-y: auto;
      .item {
        display: flex;
        padding: 15px 20px;
        align-items: center;
        border-bottom: 1px solid #F0F0F0;
        &:hover {
          cursor: pointer;
          background: #F8F8F8;
        }
        .avatar {
          width: 50px;
          height: 50px;
          line-height: 50px;
          text-align: center;
          font-size: 18px;
          border: 1px solid #F0F0F0;
          border-radius: 50%;
        }
        .info {
          padding-left: 10px;
          .name {
            margin-bottom: 6px;
            font-size: 16px;
          }
          .contact {
            display: flex;
            font-size: 12px;
            color: #666666;
            align-items: center;
            .gap {
              width: 5px;
              height: 5px;
              margin: 0 5px;
              background: #888888;
              border-radius: 50%;
            }
          }
        }
      }
    }
  }
}
</style>
